<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ include file="/WEB-INF/jsp/header.jsp" %>

<script>
	function check() {
		 var min = document.getElementById("min");
		 var max = document.getElementById("max");
		 var b = true;
		 if (min.value != -1 && max.value != -1 && parseInt(min.value) > parseInt(max.value))
		 	b = false;
		 if (!b) {
			if (document.getElementById("error") == null) {
				var p = document.createElement("P");
				p.id = "error";
				p.innerHTML = "Возраст 'от' не может быть больше 'до'";
				document.body.appendChild(p);
			} 
			document.getElementById("min").focus();
		}
		else {
			var p = document.getElementById("error"); 
			if (p != null)
				p.remove();
		}
		return b;
	}
</script>
<title>Поиск клиентов</title>
<a href="persons">
	<div class="mybutton">
		К клиентам
	</div>
</a>
<br><br><br>
<form method="POST" action="person_search" onsubmit="return check()">
	<input name="first_name" type="text" placeholder="Имя"/>
	<input name="last_name" type="text" placeholder="Фамилия"/>
	<input name="middle_name" type="text" placeholder="Отчество"/>
	<input name="phone_number" type="text" placeholder="телефон"/>
	<input name="email" type="text" placeholder="email"/>
	<input name="number" type="text" placeholder="Номер рейса"
		data-validation-optional="true"	
		data-validation="number" data-validation-allowing="range[1;1024]"/>
	<select name="min" id="min">
		<option value="-1">Возраст от</option>
		<c:forEach begin="0" end="100" varStatus="i">
			<option value="${i.index}">${i.index}</option>
		</c:forEach>
	</select>
	<select name="max" id="max">
		<option value="-1">Возраст до</option>
		<c:forEach begin="0" end="100" varStatus="i">
			<option value="${i.index}">${i.index}</option>
		</c:forEach>
	</select>
	<select name="comp" id="comp">
		<option value="-1" selected>Выберите компанию</option>
		<c:forEach items="${comps}" var="comp" varStatus="i">
			<option value="${i.index + 1}">${comp.name}</option>
		</c:forEach>
	</select>
	<br>
	<input class="button" type="submit" value="Поиск"/>
</form>

<c:if test="${persons.size() != 0}">
<table>
<tr>
	<th>ФИО</th>
	<th>Дата рождения</th>
	<th>Email</th>
	<th>Телефон</th>
</tr>
<c:forEach items="${persons}" varStatus="i" var="person">
<tbody>
	<tr>
		<td><a href="person?id=${person.id}"> ${person.first_name} ${person.middle_name} ${person.last_name}</a></td>
		<td>${strings.get(i.index)}</td>
		<td>${person.email}</td>
		<td>${person.phone_number}</td>
	</tr>
</tbody>
</c:forEach>
</table>
</c:if>
<%@ include file="/WEB-INF/jsp/footer.jsp" %>